var DormEleStatistic = function () {
  var template = /*html*/ `
    <div class="dorm-ele-statistic" v-loading.fullscreen.lock="loading">
      <div>
        <el-form :model="search" label-width="80px" :inline="true">
          <el-form-item label="住宿日期">
            <el-date-picker v-model="search.startTime" format="yyyy-MM-dd" value-format="timestamp"
              :editable="false" style="width:160px;"></el-date-picker>
            -
            <el-date-picker v-model="search.endTime" format="yyyy-MM-dd" value-format="timestamp"
              :editable="false" style="width:160px;"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchData">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="dorm-ele-statistic-content">
        <el-table class="dormStatTable" fit border stripe :data="dormStats" ref="dormStatTable" height="parent">
          <el-table-column :resizable="false" prop="dormNo" label="宿舍号码">
          </el-table-column>
          <el-table-column :resizable="false" label="用电量">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.sumEnergy)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="日平均用电量" min-width="105px">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.avgEnergy)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="最高用电量" min-width="95px">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.maxEnergy)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="最低用电量" min-width="95px">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.minEnergy)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="人均电量">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.perPeopleEnergy)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="用水量">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.sumWater)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="日平均用水量" min-width="105px">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.avgWater)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="最高用水量"  min-width="95px">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.maxWater)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="最低用水量"  min-width="95px">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.minWater)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="人均水量">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.perPeopleWater)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="电费(元)">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.electricFee)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="水费(元)">
            <template slot-scope="scope">
              <span>{{SysUtil.decimalNumber(scope.row.waterFee)}}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    data: function () {
      return {
        loading: false,
        dormStats: [],
        SysUtil: SysUtil,
        search: {
          startTime: moment().startOf('month').valueOf(),
          endTime: moment().valueOf()
        }
      };
    },
    created: function () {
      this.setDormStats();
    },
    methods: {
      setDormStats: function () {
        var search = this.search;
        if (!this.checkSearchTime(search)) {
          return;
        }

        var startTime = search.startTime;
        var endTime = search.endTime;
        var postData = {};
        if (startTime !== null) {
          postData.startTime = moment(startTime).startOf('day').valueOf();
        }
        if (endTime !== null) {
          postData.endTime = moment(endTime).endOf('day').valueOf();
        }

        this.loading = true;
        var self = this;
        ajaxRequest('dormManage/getDormStatistics', postData).then(function (dormStats) {
          self.loading = false;
          for (var i = 0; i < dormStats.length; i++) {
            var dormStat = dormStats[i];
            dormStat.sumEnergy = self.undefinedToZero(dormStat.electricalUsage);
            dormStat.sumWater = self.undefinedToZero(dormStat.waterMeterUsage);
            dormStat.avgEnergy = self.undefinedToZero(dormStat.dayElectricalUsage);
            dormStat.avgWater = self.undefinedToZero(dormStat.dayWaterMeterUsage);
            dormStat.maxEnergy = self.undefinedToZero(dormStat.maxElectricalUsage);
            dormStat.maxWater = self.undefinedToZero(dormStat.maxWaterMeterUsage);
            dormStat.minEnergy = self.undefinedToZero(dormStat.minElectricalUsage);
            dormStat.minWater = self.undefinedToZero(dormStat.minWaterMeterUsage);
            dormStat.perPeopleEnergy = self.undefinedToZero(dormStat.everyoneElectricalUsage);
            dormStat.perPeopleWater = self.undefinedToZero(dormStat.everyWaterMeterUsage);
            dormStat.electricFee = self.undefinedToZero(dormStat.electricFee);
            dormStat.waterFee = self.undefinedToZero(dormStat.waterFee);
            ///该段代码待删除，修改后的源代码，待上线代码稳定后删除 start
            // var date = Number(dormStat.date);
            // dormStat.date = date;
            //
            // var avgEnergy;
            // if (date === 0) {
            //   avgEnergy = 0;
            // } else {
            //   avgEnergy = dormStat.sumEnergy / date;
            // }
            // dormStat.avgEnergy = avgEnergy;
            //
            // var avgWater;
            // if (date === 0) {
            //   avgWater = 0;
            // } else {
            //   avgWater = dormStat.sumWater / date;
            // }
            // dormStat.avgWater = avgWater;
            //
            // var peoples = dormStat.peoples;
            // var perPeopleEnergy;
            // if (peoples === 0) {
            //   perPeopleEnergy = 0
            // } else {
            //   perPeopleEnergy = dormStat.sumEnergy / peoples;
            // }
            // dormStat.perPeopleEnergy = perPeopleEnergy;
            ///该段代码待删除，修改后的源代码，待上线代码稳定后删除 end
          }
          self.dormStats = dormStats;
        }).catch(function () {
          self.loading = false;
        });
      },

      checkSearchTime: function (search) {
        var startTime = search.startTime;
        var endTime = search.endTime;
        if (startTime !== null && endTime !== null) {
          if (moment(startTime).isAfter(endTime, 'day')) {
            this.$alert('住宿日期的开始日期不能晚于结束日期', '信息');
            return false;
          }
        }
        return true;
      },

      searchData: function () {
        this.setDormStats();
      },
      undefinedToZero: function (param) {
        if (undefined === param) {
          return 0;
        }
        return param;
      }
    },
    template: template
  };
}();